' historyplugin.bmx

Strict

Import "../maxtk/maxtk.bmx"

Type THistoryNode Extends TDataNode
	Field owner:THistoryPlugin
	Field path$
	Field archive$

	Method Invoke(command$,argument:Object=Null)
		Select command
			Case "ACTIVATE"
				DebugStop
				owner.host.OpenSource archive
		End Select
	End Method

	Method SetDetails(o:THistoryPlugin,n$,p$,a$)
		owner=o
		SetName n
		path=p
		archive=a
		SetAction(owner,"ACTIVATE",Self)
	End Method
End Type

Type THistoryPlugin Extends TPlugin

	Field	historycommand
	Field	historyroot:TDataNode
	Field	historypath$
	Field	versions:TMap
	Field	locations:TMap

	Function FindValue( list:TList,value:Object )
		Local index
		For Local value2:Object=EachIn list
			If value.Compare( value2 )=0 Return index
			index:+1
		Next			
		Return -1
	End Function

	Method Archive(path$,text$)
		Local dest$,version$,location$
		
		If historypath="" Return
		
		version=String(versions.ValueForKey(path))
		version=String(1+Int(version))
		versions.Insert path,version
		
		dest=StripAll(path)
		
		Local paths:TList
		paths=TList(locations.ValueForKey(dest))
		If Not paths
			paths=New TList
			locations.Insert dest,paths
		EndIf
		
		Local index=FindValue(paths,path)+1
		If index=0
			paths.AddLast path
			index=paths.Count()
		EndIf	
		location=String(index)
			
		dest=StripAll(path)
		dest:+"_"+location
		dest:+"_"+version
		dest:+"."+ExtractExt(path)		

		dest$=historypath+"/"+dest
		SaveText text,dest

'		DebugLog "history:path="+path+"~ntext="+text
		
		Local n:THistoryNode
		Local o:THistoryNode
		Local filename$

		filename=StripDir(path)

		n=THistoryNode(historyroot.FindNode(filename))	
		If Not n n=New THistoryNode
		n.setdetails(Self,filename,"",path$)
		
		o=New THistoryNode
		o.setdetails(Self,filename+"("+version+")",path,dest$)
		n.AddFirst o
	
		historyroot.AddFirst n
		historyroot.Refresh
		
		WriteHistory
	End Method

'Historyrequester.Show

	Method Initialize(owner:THost)
		host=owner
		SetName("History")
		historycommand=AddCommand("&Tools","&History",KEY_H,MODIFIER_CONTROL|MODIFIER_SHIFT)			
		historyroot=TDataNode.Create("History")
'		historyroot.Open
		host.AddNode historyroot
		host.SetArchiver Self
		historypath=host.apppath+"/history"		
		If FileType(historypath)=0 
			If Not CreateDir(historypath) Notify "Unable to create history directory "+historypath
		EndIf
		SetProperty "HistoryDir",historypath				
		versions=New TMap
		locations=New TMap
		ReadHistory
		historyroot.Refresh
	End Method

	Method ReadHistory()
		Local path$,vers$
		Local dest$,destlist:TList,d$
		Local node:THistoryNode
		Local nodee:THistoryNode
		Local n$,p$,a$
		
		If Not historypath Return
		Local f:TStream=ReadFile(historypath+"/history.txt")
		If f 
			While Not Eof(f)
				Local l$=ReadLine(f)
				Select l
					Case "version"
						path$=ReadLine(f)
						vers$=ReadLine(f)
						ReadLine f
						versions.Insert path,vers
					Case "location"					
						dest$=ReadLine(f)
						destlist=New TList
						While True
							d$=ReadLine(f)
							If Not d$ Exit
							destlist.AddLast d
						Wend
					Case "history"
						n$=ReadLine(f)
						p$=ReadLine(f)
						a$=ReadLine(f)
						node=New THistoryNode
						node.setdetails Self,n,p,a
						historyroot.Append node
						While True
							n$=ReadLine(f)
							If Not n Exit
							p$=ReadLine(f)
							a$=ReadLine(f)
							nodee=New THistoryNode
							nodee.setdetails Self,n,p,a
							node.Append nodee
						Wend				
				End Select
			Wend
			CloseStream f
		EndIf
	End Method
	
	Method WriteHistory()
		If Not historypath Return
		Local f:TStream=WriteFile(historypath+"/history.txt")
		If f 
'versions		
			Local path$
			For path=EachIn versions.keys()
				WriteLine f,"version"
				WriteLine f,path
				WriteLine f,String(versions.ValueForKey(path))
				WriteLine f,""
			Next
'locations
			Local dest$,loc$
			Local paths:TList
			For dest$=EachIn locations.keys()
				WriteLine f,"location"
				WriteLine f,dest
				paths=TList(locations.ValueForKey(dest))
				For loc=EachIn paths
					WriteLine f,loc
				Next
				WriteLine f,""
			Next
'historyroot			
			Local nodee:THistoryNode			
			Local node:THistoryNode			
			For node=EachIn historyroot.kids
				WriteLine f,"history"
				WriteLine f,node.name
				WriteLine f,node.path
				WriteLine f,node.archive
				For nodee=EachIn node.kids
					WriteLine f,nodee.name
					WriteLine f,nodee.path
					WriteLine f,nodee.archive
				Next			
				WriteLine f,""
			Next
			CloseStream f
		EndIf
	End Method

	Method Invoke(command$,argument:Object=Null)	
		Select command
			Case "RESET"	'new for 9.06
			Case "ARCHIVE"
				Local pathdata:Object[]=Object[](argument)
				If Not pathdata Or pathdata.length<>2 Return
				Local path$=String(pathdata[0])
				Local text$=String(pathdata[1])
				Archive path,text
			Case "HISTORY"
				' do history requester
				Notify "History Requester coming soon..."		
			Default
				Notify "history invoke with unknown command "+command
		End Select
	End Method
		
	Method Open:Object(path$)
	End Method

End Type



Rem		
	method FixHistory()
		Local files$[]
		Local t$
		
		files=LoadDir(historypath)
		For t$=EachIn files
			Local p$=historypath+"/"+t
			If FileType(p)=1
				DebugLog "historydir:"+p
			EndIf
		Next
	end method
EndRem		

